package com.example.carrierapp.ui.adapter;

import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.carrierapp.bean.home.NoticeListBean;
import com.example.carrierapp.ui.view.recycleview.XRecyclerView;
import com.example.carrierapp.util.DateUtils;
import com.example.lb.fingerdemo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NoticeAdapter extends XRecyclerView.Adapter<NoticeAdapter.ViewHolder> {
    private List<NoticeListBean.ItemsBean> mList = new ArrayList();
    private int mClickIndex;

    public NoticeAdapter() {
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_notice, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        NoticeListBean.ItemsBean info = mList.get(position);
        holder.bindData(info);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void addDataNotClear(List<NoticeListBean.ItemsBean> list) {
        if (list != null && list.size() > 0) {
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void addData(List<NoticeListBean.ItemsBean> list) {
        if (list != null && list.size() > 0) {
            mList.clear();
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void updataItemData(NoticeListBean.ItemsBean bean) {
        mList.remove(mClickIndex);
        notifyItemRemoved(mClickIndex);

        mList.add(mClickIndex, bean);
        notifyItemInserted(mClickIndex);

        notifyDataSetChanged();
    }


    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    public class ViewHolder extends XRecyclerView.ViewHolder {
        @BindView(R.id.tv_notice_msg)
        TextView mTvMsg;
        @BindView(R.id.text_time)
        TextView mTvTime;
        @BindView(R.id.tv_notice_detail)
        TextView mTvDetail;

        @BindView(R.id.iv_notice_new)
        ImageView mIvNew;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(NoticeListBean.ItemsBean info) {
            mTvMsg.setText(info.title);
            mTvTime.setText(DateUtils.getTimeInterval(info.creationTime));
            mTvDetail.setText(info.subTitle);
            mIvNew.setVisibility(info.isSee ? View.GONE : View.VISIBLE);

            mTvDetail.setVisibility(info.isShowDetail ? View.VISIBLE : View.GONE);
            itemView.setOnClickListener(v -> {
                if (info.isShowDetail) {
                    mTvDetail.setVisibility(View.GONE);
                } else {
                    mIvNew.setVisibility(View.GONE);

                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onDetailClick(info);
                    }
                }

            });
        }
    }

    public interface OnBtnClickListener {
        void onDetailClick(NoticeListBean.ItemsBean info);
    }

    private OnBtnClickListener mOnItemClickListener;

    public void setOnBtnClickListener(OnBtnClickListener listener) {
        mOnItemClickListener = listener;
    }
}
